﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace badminton.Pages
{
    public partial class HallDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int hallId = -1;
            if (this.Request.QueryString["hallid"] != null)
            {
                hallId = Convert.ToInt32(this.Request.QueryString["hallid"]);
            }
            BadmintonEntities bde = new BadmintonEntities();
            if (hallId==-1)
            {
                Hall hh = bde.Hall.FirstOrDefault();
                if (hh!=null)
                {
                    hallId = hh.id;
                }
            }
            Hall hall = bde.Hall.FirstOrDefault(p => p.id == hallId);

            int selIdx = 0;
            foreach (CityArea ca in bde.CityArea)
            {
                AjaxControlToolkit.AccordionPane panel = new AjaxControlToolkit.AccordionPane();
                panel.ID = "AccordPane" + ca.id.ToString();
                panel.HeaderContainer.Controls.Add(new LiteralControl(ca.name));

                foreach (Hall hl in ca.Hall.OrderBy(p => p.id))
                {
                    HyperLink hlChildMenu = new HyperLink();
                    hlChildMenu.Text = hl.name + "<br/>";
                    hlChildMenu.NavigateUrl = "~\\Pages\\HallDetail.aspx?hallid=" + hl.id.ToString();
                    panel.ContentContainer.Controls.Add(hlChildMenu);
                }
                if (hall != null && hall.CityArea != null)
                {
                    if (ca.id == hall.cityAreaId)
                    {
                        selIdx = MyAccordion.Panes.Count;
                    }
                }
                MyAccordion.Panes.Add(panel);
            }

            MyAccordion.SelectedIndex = selIdx;

            if (!IsPostBack)
            {


                if (hall == null)
                {
                    return;
                }

                Label1.Text = hall.CityArea != null ? hall.CityArea.name : "";
                Label2.Text = hall.name;

                Editor1.Content = hall.description;

                // trainers
                DataTable tbl = new DataTable();
                tbl.Columns.Add("id");
                tbl.Columns.Add("name");
                DataRow drow;
                foreach(Training tr in hall.Training)
                {
                    if (tr.Teacher==null)
                        continue;

                    drow = tbl.NewRow();
                    drow["id"] = tr.Teacher.id;
                    drow["name"] = tr.Teacher.name;
                    tbl.Rows.Add(drow);
                }
                RepeaterTrainer.DataSource = tbl;
                RepeaterTrainer.DataBind();

                if (hall.hallPhoto != null)
                {
                    this.Image1.ImageUrl = "~/DynamicData/images/" + hall.hallPhoto;
                }
                if (hall.mapPhoto != null)
                {
                    this.Image2.ImageUrl = "~/DynamicData/images/" + hall.mapPhoto;
                }
            }

        }
    }
}